< previous page page_124 next page >

Page 124
What Are Objects and Collections?
Before you can understand what a collection is, you must know what an object is and what makes it a useful tool when you are developing Visual Basic applications. Objects are the central concept to Visual Basic programming. Forms, controls, and databases are considered objects. In fact, everywhere you look in Visual Basic you will see objects. When you start working directly with these objects, you need some way to keep track of them. In Visual Basic, a special object called a collection enables you to keep track of all the other objects that are in your application. A collection is a way to group a set of related items. In Visual Basic, collections are used to keep track of many things, such as the forms that are loaded in your program or the controls that are on a form.
Objects
Almost everything you use in Visual Basic can be considered an object. In fact, you have already worked with many objects in this book. Every control in Visual Basic is an object. When working with controls, you have seen that they all contain properties, methods, and events. Each control also contains code and data items, although you can't see the actual code that was written. In fact, each control or object, such as the TextBox control, is really a small self-contained package that is included with Visual Basic.
When you use the text box, you don't have to write any code to trigger its events or define any of its properties. The text box methods do all the work that you will need. This process is termed encapsulation. An object is like a space capsule; everything it needs to perform its task is included with the object. All the methods, events, and properties that an object uses are defined or coded within the object itself. All objects that share the same features (such as many text boxes on one form) are included in an object class. A single object is really an instance of its class definition. Every time you add a new control to a form, you are really creating a new instance of its class. If you go back and look at the code that you placed in the MDI project on Day 3, Changing the Face of the Application, you will see the definition of a form object as shown in the following:
Dim NewNote As New frmNote
This code defines a new object call NewNote that is based on the defined form frmNote object. You might not realize it, but when you design a form and place controls on it, you are creating your own object. By using existing classes, you automatically inherit the properties, methods, and events from that class. To see this, you need go no further than the remaining code from the mnuFileNew_Click event from the MDI project as shown in the following:
intFormCtr = intFormCtr + 1
    NewNote.Show
    NewNote.Caption = NotePad Version  & intFormCtr

 
< previous page page_124 next page >

If you like this book, buy it!